﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "UIWelcome.h"

#include "QMdiArea.h"
#include "GmMainWindow.h"
#include "IUiDocumentViewManager.h"

#include <QCoreApplication>

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
//////////////////////////////////////////////////////////////////////////


WelcomeWidget::WelcomeWidget(QWidget *parent)
    :QWidget(parent, Qt::FramelessWindowHint| Qt::SubWindow)
{
}

UIWelcome::UIWelcome(IMainWindow* pUIMainWnd) : m_pUIMainWnd(pUIMainWnd)
{
}

UIWelcome::~UIWelcome(void)
{
}

void UIWelcome::UpdateUI(QResizeEvent *event)
{
    if (m_pUIMainWnd == NULL)
        return;
    GmMainWindow* mainWindow = dynamic_cast<GmMainWindow*>(m_pUIMainWnd);
    if (mainWindow == NULL)
        return;
    UInt64 id = mainWindow->GetMdiAreaId();
    QMdiArea* pMDIArea = dynamic_cast<QMdiArea*>(QWidget::find((WId)id));
    DBG_WARN_AND_RETURN_VOID_UNLESS(pMDIArea, L"pMDIArea为空",L"GDMPLab",L"2024-03-30");
    for (int i = 0; i < UIWelcomeManager::Get()->GetQWidgetsCount(); i++)
    {
        if (UIWelcomeManager::Get()->GetQWidgets(i) == nullptr)
            continue;
        WelcomeWidget* info = UIWelcomeManager::Get()->GetQWidgets(i);
        if (info->parent() == nullptr)
            info->setParent(pMDIArea);
        if (UIWelcomeManager::Get()->GetQWidgets(i)->isHidden())
            continue;
        QRect mR = mainWindow->rect();
        QRect r = pMDIArea->viewport()->rect();
        info->resize(mR.width(), r.height());
        info->UpdateUI();
    }
}

void UIWelcome::Show()
{
    if (m_pUIMainWnd == NULL)
        return;
    GmMainWindow* mainWindow = dynamic_cast<GmMainWindow*>(m_pUIMainWnd);
    if (mainWindow == NULL)
        return;
    if (!IUiDocumentViewManager::Get()->GetAllUiDocuments().empty())
        return;

    UInt64 id = mainWindow->GetMdiAreaId();
    QMdiArea* pMDIArea = dynamic_cast<QMdiArea*>(QWidget::find((WId)id));
    DBG_WARN_AND_RETURN_VOID_UNLESS(pMDIArea, L"pMDIArea为空",L"GDMPLab",L"2024-03-30");
    if (pMDIArea != nullptr&&pMDIArea->viewport() != nullptr)
    {
        for (int i = 0; i < UIWelcomeManager::Get()->GetQWidgetsCount(); i++)
        {
            if (UIWelcomeManager::Get()->GetQWidgets(i) == nullptr)
                continue;
            WelcomeWidget* info = UIWelcomeManager::Get()->GetQWidgets(i);
            if (info->parent() == nullptr)
                info->setParent(pMDIArea);
            if (!UIWelcomeManager::Get()->GetQWidgets(i)->isHidden())
                continue;

            QRect mR = mainWindow->rect();
            QRect r = pMDIArea->viewport()->rect();
            info->resize(mR.width(), r.height());
            int w = (int)info->width();
            int h = (int)info->height();
            info->show();
            info->UpdateUI();
        }
    }
}

void UIWelcome::Hide()
{
    if (m_pUIMainWnd == NULL)
        return;
    GmMainWindow* mainWindow = dynamic_cast<GmMainWindow*>(m_pUIMainWnd);
    if (mainWindow == NULL)
        return;

    UInt64 id = mainWindow->GetMdiAreaId();
    QMdiArea* pMDIArea = dynamic_cast<QMdiArea*>(QWidget::find((WId)id));
    DBG_WARN_AND_RETURN_VOID_UNLESS(pMDIArea, L"pMDIArea为空",L"GDMPLab",L"2024-03-30");
    if (pMDIArea != nullptr&&pMDIArea->viewport() != nullptr)
    {
        for (int i = 0; i < UIWelcomeManager::Get()->GetQWidgetsCount(); i++)
        {
            if (UIWelcomeManager::Get()->GetQWidgets(i) == nullptr)
                continue;
            if (UIWelcomeManager::Get()->GetQWidgets(i)->isHidden())
                continue;

            UIWelcomeManager::Get()->GetQWidgets(i)->hide();
            QCoreApplication::processEvents();
        }
    }
}

void UIWelcome::UpdateUI()
{
    for (int i = 0; i < UIWelcomeManager::Get()->GetQWidgetsCount(); i++)
    {
        if (UIWelcomeManager::Get()->GetQWidgets(i) == nullptr)
            continue;
        UIWelcomeManager::Get()->GetQWidgets(i)->UpdateUI();
    }
}

UIWelcomeManager* UIWelcomeManager::Get()
{
    static UIWelcomeManager mng;
    return &mng;
}

// 得到QWidgets
int UIWelcomeManager::GetQWidgetsCount() const
{
    return (int)m_upPages.size();
}

//创建，用于初始化欢迎界面
void UIWelcomeManager::AddQWidget(WelcomeWidget* childWidget, const std::wstring& widgetName)
{
    WidgetInfo info;
    info.m_widget = childWidget;
    info.m_name = widgetName;
    m_upPages.push_back(info);
}

void UIWelcomeManager::SetQWidget(WelcomeWidget* childWidget, const std::wstring& widgetName)
{
    ClearQWidget();
    AddQWidget(childWidget, widgetName);
}

UIWelcomeManager::~UIWelcomeManager()
{
    ClearQWidget();
}

//清除WelcomeWidget
void UIWelcomeManager::ClearQWidget()
{
    m_upPages.clear();
}

// 得到QWidgets
WelcomeWidget* UIWelcomeManager::GetQWidgets(const std::wstring& widgetName)
{
    for (int i = 0; i < GetQWidgetsCount(); i++)
    {
        if (m_upPages.at(i).m_widget == nullptr)
            continue;
        if (m_upPages.at(i).m_name == widgetName)
            return m_upPages.at(i).m_widget;
    }
    return nullptr;
}

WelcomeWidget* UIWelcomeManager::GetQWidgets(const int& widgetIndex)
{
    if (widgetIndex >= GetQWidgetsCount())
        return nullptr;
    return m_upPages.at(widgetIndex).m_widget;
}


